package com.example.oa.config;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author 王令
 * @date 2022-04-26 17-07-07
 * @desc
 */
@Configuration
public class DingtalkConfig {

    @Value("${com.example.dingtalk.app-key}")
    private String appKey;
    @Value("${com.example.dingtalk.app-secret}")
    private String appSecret;
    @Value("${com.example.dingtalk.server-url}")
    private String serverUrl;

    @Bean
    @Scope(scopeName = "request")
    public OapiGettokenResponse oapiGettokenResponse() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(serverUrl);
        OapiGettokenRequest req = new OapiGettokenRequest();
        req.setAppkey(appKey);
        req.setAppsecret(appSecret);
        req.setHttpMethod("GET");
        return client.execute(req);
    }
}
